#!/usr/bin/python
# -*- coding:utf-8 -*-
# @time: 2021/5/25 4:25 下午


import re

DOMAIN_RE = re.compile(
    r'^[a-zA-Z0-9*][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})*\.[a-zA-Z]{2,8}(?::\d{2,5})?$'
)
SECOND_LEVEL_DOMAIN_RE = re.compile(r'^[A-Za-z0-9][A-Za-z0-9-]{0,62}$')
HOST_RE = re.compile(r'^(?![-])[\da-zA-Z-_]{1,50}(?<![-])$')
IP_RE = re.compile(
    r'^((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]\d)|[1-9])(\.((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]\d)|\d)){3}$'
)
IP_PORT_RE = re.compile(
    r'^((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]\d)|[1-9])(\.((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]\d)|\d)){3}(?::\d{2,5})?$'
)
IP_SEGMENT_RE = re.compile(
    r'^((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]\d)|\d)(\.((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]\d)|\d)){2}$'
)
HOTLINK_RE = re.compile(
    r'^(\*\.)?[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})*\.[a-zA-Z]{2,8}|((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]\d)|\d)(\.((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]\d)|\d)){3}$'
)
STAT_PASSWORD_RE = re.compile(r'^[\w]{6,12}$')
SUFFIX_RE = re.compile(r'^[a-z]{1,10}([.][a-z]{1,10})?$')
KEYWORD_RE = re.compile(r'^[\w\s\/.!@#$%^&*()+=_\'\":;?\\\\-]+$')
URL_LIST_RE = re.compile(r'^[\w\s\/.!@#$%^&*()+=_|\-\'\":;?\\\{\}\[\]\<\>,~`]+$')
URL_RE = re.compile(r'[\w\s\/.!@#$%^&*+=_\'\":;?-]+')
USER_EMAIL_RE = re.compile(
    r'^[A-Za-z0-9][-_A-Za-z0-9\.]*@([-A-Za-z0-9]+\.)+[A-Za-z0-9]{2,32}'
)
# 刷新缓存url参数
PARAM_RE = re.compile(r'^[A-Za-z0-9/_&#$%*=.:;?@-]*$')

# 防黑锁匹配规则
HACKER_URL_RE = re.compile(r'[\w\s\/.!@#$%^&*+=_\'\":;?-]*')

# 自定义关键词过滤
FILTER_CUSTOMIZE = re.compile(r"^([\u4e00-\u9fa5]|\w){1,10}$")

# 羊毛盾关键字检测
POST_FILTER_URL_RE = re.compile(r'^[\w\s\/.!@#$%^&*+=_\'\":;?-]+$')

# 中英文数字下划线
NAME_RE = re.compile(r"^([\u4e00-\u9fa5]|\w)*$")

# 净网盾关键词
SHIELD_RE = re.compile(r"^([\u4e00-\u9fa5]|[A-Za-z0-9]|[\*])*$")

DOMAIN_HOST_RE = re.compile(
    r'^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$'
)

# 精准访问控制自定义请求头字段
CUSTOMER_HEADER = re.compile(r"^([A-Za-z0-9]|[-])*$")

# CORS规则来源
CORS_RE = re.compile(
    r'[-a-zA-Z0-9\*]{0,62}(\.[-a-zA-Z0-9\*]{0,62})*\.[a-zA-Z*]{2,8}|(([2\*][5\*][0-5\*])|([2\*][0-4\*][\d\*])|([1\*][\d\*]{2})|([1-9\*][\d\*])|\d)(\.(([2\*][5\*][0-5\*])|([2\*][0-4\*][\d\*])|([1\*][\d\*]{2})|([1-9\*][\d\*])|[\d\*])){3}$'
)
# 路径验证
PATH_RE = re.compile(r'^\/(\w+\/?)+$')

PORT_RE = re.compile(r':[\d]{2,5}')
SCHEMA_RE = re.compile(r'^https://|http://')

# 邮箱格式
G_RE_EMAIL = re.compile(
    # dot-atom
    r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*"
    # quoted-string
    r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*"'
    # domain
    r')@(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?$',
    re.IGNORECASE,
)

IPV6_RE = re.compile(
    r"^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$"
)

# special characters
SPECIAL_CHA_SET = {'*', '.', '?', '+', '$', '^', '[', ']', '(', ')', '{', '}', '|'}
NAME_REPLACE_CHA_SET = {'\n', '\t'}

# letter only
LETTER_RE = re.compile(r"^([a-zA-Z])*$")

PHONE_RE = re.compile(r'^1[3-9]\d{9}$')

REPLACE_PROTOCOL_RE = re.compile(r"http://|https://|\[|\]")
